package com.server.controller;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.server.common.base.SessionData;
import com.server.common.util.JSONUtil4TSY;
import com.server.service.ICommonDataService;
import com.server.service.IRewardConfigService;
import com.server.web.DataVector;
import com.server.web.JSONSerielizer;
import com.server.web.OutputUtil;
import com.server.web.RequestInput;
import com.server.web.SessionUtil;

@Controller
@RequestMapping("common")
public class CommonDataController {
	
	@Autowired
	private ICommonDataService commonDataService;
	@Autowired
	private IRewardConfigService rewardConfigService;
	
	 /**
	 * 各种分类列表
	 * @param request
	 * @param response
	 * @param category
	 * @author ServerZhang
	 * @date 2017年3月6日
	 */
	@RequestMapping("type/{category}")
	public void type(HttpServletRequest request,
			HttpServletResponse response, @PathVariable String category){
		List<Map<String, Object>> type = commonDataService.getType("reward."+category);
		OutputUtil.returnString(response, JSONSerielizer.toJson(type, null, null));
	}
	
	@RequestMapping("province")
	public void province(HttpServletRequest request,HttpServletResponse response){
		List<Map<String, Object>> provinceData = commonDataService.provinceData();
		OutputUtil.returnString(response, JSONSerielizer.toJson(provinceData, null, null));
	}
	@RequestMapping("rewardPolicy/{orgNo}/{nature}/{sourceType}/{employTime}")
	public void rewardPolicy(HttpServletRequest request,HttpServletResponse response,
			@PathVariable String orgNo,@PathVariable String nature,@PathVariable String sourceType,@PathVariable String employTime){
		List<Map<String, Object>> reward = null;
		if(!StringUtils.isEmpty(sourceType)){
			reward=rewardConfigService.getList(orgNo,nature,sourceType,employTime);
		}
		OutputUtil.returnString(response, JSONSerielizer.toJson(reward, null, null));
	}
	@RequestMapping("rewardPolicyDetail/{rewardId}")
	public void rewardPolicyDetail(HttpServletRequest request,HttpServletResponse response,
			@PathVariable String rewardId){
		Map<String, Object> reward = null;
		if(!StringUtils.isEmpty(rewardId)){
			reward=rewardConfigService.getForm(rewardId);
		}
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		list.add(reward);
		OutputUtil.returnString(response, JSONSerielizer.toJson(list, null, null));
	}
	
	@RequestMapping("city/{father}")
	public void city(HttpServletRequest request,
			HttpServletResponse response,@PathVariable String father){
		if(!StringUtils.isEmpty(father)){
			List<Map<String, Object>> provinceData = commonDataService.cityData(father);
			OutputUtil.returnString(response, JSONSerielizer.toJson(provinceData, null, null));
		}else{
			OutputUtil.returnString(response, JSONSerielizer.toJson("", null, null));
		}
	}
	@RequestMapping("area/{father}")
	public void area(HttpServletRequest request,
			HttpServletResponse response,@PathVariable String father){
		if(!StringUtils.isEmpty(father)){
			List<Map<String, Object>> provinceData = commonDataService.areaData(father);
			OutputUtil.returnString(response, JSONSerielizer.toJson(provinceData, null, null));
		}else{
			OutputUtil.returnString(response, JSONSerielizer.toJson("", null, null));
		}
	}
	
	
	@RequestMapping("app/province")
	public void province4App(HttpServletRequest request,HttpServletResponse response){
		String cardId = SessionUtil.getStringAttri(request,
				SessionData.cardIdKey);
		String userName = SessionUtil.getStringAttri(request,
				SessionData.nameKey);
		Integer level = SessionUtil.getIntAttri(request,
				SessionData.levelKey);
		String errInfor = "";
		if (StringUtils.isEmpty(cardId) || StringUtils.isEmpty(userName)) {
			errInfor = "未登录";
		}

		response.addHeader("Access-Control-Allow-Origin",
				request.getHeader("Origin"));
		response.addHeader("Access-Control-Allow-Credentials", "true");
		response.addHeader("Content-Type", "application/json; charset=utf-8");
		RequestInput requestInput = new RequestInput(request);
		List<Map<String, Object>> provinceData = commonDataService.provinceData();
		String list2Json = JSONUtil4TSY.list2Json(level,provinceData,
				provinceData.size(), requestInput.getPageSize(),
				requestInput.getPageIndex() + 1, cardId, userName,
				response.getStatus(), errInfor);
		OutputUtil.returnString(response, list2Json);
	}
	@RequestMapping("app/city/{father}")
	public void city4App(HttpServletRequest request,
			HttpServletResponse response,@PathVariable String father){
		String cardId = SessionUtil.getStringAttri(request,
				SessionData.cardIdKey);
		String userName = SessionUtil.getStringAttri(request,
				SessionData.nameKey);
		Integer level = SessionUtil.getIntAttri(request,
				SessionData.levelKey);
		String errInfor = "";
		if (StringUtils.isEmpty(cardId) || StringUtils.isEmpty(userName)) {
			errInfor = "未登录";
		}

		response.addHeader("Access-Control-Allow-Origin",
				request.getHeader("Origin"));
		response.addHeader("Access-Control-Allow-Credentials", "true");
		response.addHeader("Content-Type", "application/json; charset=utf-8");
		RequestInput requestInput = new RequestInput(request);
		List<Map<String, Object>> provinceData = commonDataService.cityData(father);
		String list2Json = JSONUtil4TSY.list2Json(level,provinceData,
				provinceData.size(), requestInput.getPageSize(),
				requestInput.getPageIndex() + 1, cardId, userName,
				response.getStatus(), errInfor);
		OutputUtil.returnString(response, list2Json);
	}
	@RequestMapping("app/area/{father}")
	public void area4App(HttpServletRequest request,
			HttpServletResponse response,@PathVariable String father){
		String cardId = SessionUtil.getStringAttri(request,
				SessionData.cardIdKey);
		String userName = SessionUtil.getStringAttri(request,
				SessionData.nameKey);
		Integer level = SessionUtil.getIntAttri(request,
				SessionData.levelKey);
		String errInfor = "";
		if (StringUtils.isEmpty(cardId) || StringUtils.isEmpty(userName)) {
			errInfor = "未登录";
		}

		response.addHeader("Access-Control-Allow-Origin",
				request.getHeader("Origin"));
		response.addHeader("Access-Control-Allow-Credentials", "true");
		response.addHeader("Content-Type", "application/json; charset=utf-8");
		RequestInput requestInput = new RequestInput(request);
		List<Map<String, Object>> provinceData = commonDataService.areaData(father);
		String list2Json = JSONUtil4TSY.list2Json(level,provinceData,
				provinceData.size(), requestInput.getPageSize(),
				requestInput.getPageIndex() + 1, cardId, userName,
				response.getStatus(), errInfor);
		OutputUtil.returnString(response, list2Json);
	}

}
